﻿using System;
using System.Windows.Forms;
using dk.Helper.Base;
using dk.Helper.Base.Core;

namespace dk.Helper.Controls
{
    public partial class WorldControl : UserControl
    {
        private readonly LoadingForm loadingForm;
        private readonly IWorldDownloader worldDownloader;
        private readonly IWorldInitializer worldInitializer;

        /// <summary>
        /// Initializes a new instance of the <see cref="WorldControl"/> class.
        /// </summary>
        /// <param name="world_in">The world.</param>
        public WorldControl(IWorld world_in)
        {
            World = world_in;
            InitializeComponent();
            loadingForm = new LoadingForm();

            worldDownloader = new WorldDownloader(World.Name, World.Address);
            worldDownloader.DownloadProgressChanged += WorldDownloader_DownloadProgressChanged;
            worldDownloader.DownloadCompleted += WorldDownloader_DownloadCompleted;

            worldInitializer = new WorldInitializer();

            btnWorld.Text = World.Name.Insert(2, Environment.NewLine);
        }

        /// <summary>
        /// Gets the world.
        /// </summary>
        public IWorld World { get; private set; }

        /// <summary>
        /// Handles the Click event of the btnWorld control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnWorld_Click(object sender, EventArgs e)
        {
            loadingForm.Show();
            worldDownloader.Download();
        }

        /// <summary>
        /// Handles the DownloadProgressChanged event of the WorldDownloader control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="dk.Helper.Base.ProgressEventArgs"/> instance containing the event data.</param>
        private void WorldDownloader_DownloadProgressChanged(object sender, ProgressEventArgs e)
        {
            loadingForm.ProgressBar.Value = e.Percentage;
        }

        /// <summary>
        /// Handles the DownloadCompleted event of the WorldDownloader control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void WorldDownloader_DownloadCompleted(object sender, EventArgs e)
        {
            worldInitializer.Init(World);
            loadingForm.Close();
            OnWorldInitialized(new EventArgs());
        }

        /// <summary>
        /// Occurs when world is initialized.
        /// </summary>
        public event EventHandler WorldInitialized;

        /// <summary>
        /// Raises the <see cref="WorldInitialized"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void OnWorldInitialized(EventArgs e)
        {
            if (WorldInitialized != null) WorldInitialized(this, e);
        }
    }
}